package encoding.CharSet;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import util.PathUtil;



public class ANSI2UTF8 {

	/**
	 * @param args
	 * @throws UnsupportedEncodingException
	 */
	public static void main(String[] args) throws UnsupportedEncodingException {
		InputStream in = null;
		byte[] bytes = new byte[40];

		try {
			// 文件编码为GBK
			in = new FileInputStream(PathUtil.getCurrentClasPath(ANSI2UTF8.class) + "\\test.txt");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

		if (in != null) {
			try {
				in.read(bytes, 0, 40);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		// 乱码
		String utf8Str = new String(bytes, "utf-8");
		byte[] utf8StrBytes = utf8Str.getBytes("utf-8");
		System.out.println("String from UTF-8: "
				+ new String(utf8StrBytes, "utf-8"));

		String gbkStr = new String(bytes, "gbk");
		byte[] gbkStrBytes = gbkStr.getBytes("gbk");
		System.out
				.println("String from GBK: " + new String(gbkStrBytes, "gbk"));

		String iso8859Str = new String(bytes, "ISO-8859-1");
		byte[] iso8859StrBytes = iso8859Str.getBytes("ISO-8859-1");
		System.out.println("String from ISO-8859-1: "
				+ new String(iso8859StrBytes, "gbk"));

		System.out.println("---------------------------------------------");

		String str1 = "a中文";
		byte[] str1GBKBytes = str1.getBytes("GBK");
		String str1GBK = new String(str1GBKBytes, "GBK");
		System.out.println(str1GBK);

		byte[] str1UTF8Bytes = str1.getBytes("utf-8");
		String str1UTF8 = new String(str1UTF8Bytes, "utf-8");
		System.out.println(str1UTF8);

		byte[] str18859Bytes = str1.getBytes("ISO-8859-1");
		String str18859 = new String(str18859Bytes, "ISO-8859-1"); // 乱码
		System.out.println(str18859);
	}
}